var shares=null;

// H5 plus事件处理
function plusReady(){
	updateSerivces();
}
if(window.plus){ 
	plusReady();
}else{
	 
	 document.addEventListener('plusready', plusReady, false);
}
/**
 * 更新分享服务
 */
function updateSerivces(){
	plus.share.getServices(function(s){
		shares={};
		for(var i in s){
			var t=s[i];
			shares[t.id]=t;
		}
	}, function(e){
		mui.toast('获取分享服务列表失败：'+e.message);
	});
}
// 分析链接
function shareapp(){
	
	var shareBts=[];
	// 更新分享列表
	var ss=shares['weixin'];
	ss&&ss.nativeClient&&(shareBts.push({title:'微信朋友圈',s:ss,x:'WXSceneTimeline'}),
	shareBts.push({title:'微信好友',s:ss,x:'WXSceneSession'}));
	/*ss=shares['qq'];
	ss&&ss.nativeClient&&shareBts.push({title:'QQ',s:ss});*/
	ss=shares['sinaweibo'];
	ss&&shareBts.push({title:'新浪微博',s:ss});
	// 弹出分享列表
	shareBts.length>0?plus.nativeUI.actionSheet({title:'分享寐淘',cancel:'取消',buttons:shareBts},function(e){
		(e.index>0)&&shareAction(shareBts[e.index-1],true);
	}):plus.nativeUI.alert('当前环境无法支持分享链接操作!');
}
//分享商品
function sharepro(){
	
	
	var shareBts=[];
	// 更新分享列表
	var ss=shares['weixin'];
	ss&&ss.nativeClient&&(shareBts.push({title:'微信朋友圈',s:ss,x:'WXSceneTimeline'}),
	shareBts.push({title:'微信好友',s:ss,x:'WXSceneSession'}));
//	ss=shares['qq'];
//	ss&&ss.nativeClient&&shareBts.push({title:'QQ',s:ss});
	ss=shares['sinaweibo'];
	ss&&shareBts.push({title:'新浪微博',s:ss});
	
	// 弹出分享列表
	shareBts.length>0?plus.nativeUI.actionSheet({title:'分享寐淘',cancel:'取消',buttons:shareBts},function(e){
		(e.index>0)&&shareProAction(shareBts[e.index-1],true);
	}):plus.nativeUI.alert('当前环境无法支持分享链接操作!');
} 
 
/**
   * 分享操作
   * @param {JSON} sb 分享操作对象s.s为分享通道对象(plus.share.ShareService)
   * @param {Boolean} bh 是否分享链接
   */
function shareProAction(sb,bh) {
	// var url=$("#ifdetail").attr("src");
	 var url=localStorage.plink;
	if(!sb||!sb.s){
			mui.toast('无效的分享服务！');
		return;
	}
	var msg={content:"寐淘在手，折扣尽有",extra:{scene:sb.x}};
	if(bh){
		msg.href= "http://www.maytao.cn/share/detailpro?pid="+ localStorage.pid;
		//console.log(msg.href)
	   if(sb.title=="新浪微博")
	   {
	   	msg.title="#寐淘##折扣总汇##省钱利器#"+localStorage.prisour+"  "+localStorage.title+"http://www.maytao.cn/down/maytao.apk";
	   }
	   else{
		msg.title=localStorage.prisour+"  "+localStorage.title;}
		msg.content=msg.title;
	 
		msg.thumbs=[localStorage.imgurl];
		msg.pictures=['imgs/code.png'];
	}else{
		msg.pictures=[localStorage.imgurl];
		 
	}
	// 发送分享
	if(sb.s.authenticated){ 
		shareMessage(msg, sb.s);
	}else{
		//	mui.toast('---未授权---');
		sb.s.authorize(function(){
			shareMessage(msg,sb.s);
		}, function(e){
			//	mui.toast('认证授权失败：'+e.code+' - '+e.message);
		});
	}
}


/**
   * 分享操作
   * @param {JSON} sb 分享操作对象s.s为分享通道对象(plus.share.ShareService)
   * @param {Boolean} bh 是否分享链接
   */
function shareAction(sb,bh) {
	//mui.toast('分享操作：');
	if(!sb||!sb.s){
			//mui.toast('无效的分享服务！');
		return;
	}
	var msg={content:"寐淘在手，折扣尽有",extra:{scene:sb.x}};
	if(bh){
		msg.href= "http://www.maytao.cn/share/index" //sharehref.value;
		 
		msg.title="我发现一个好多折扣商品的app,快来看看吧，好多优惠哦！"
		msg.content="这是一个聚集折扣商品的app,值得你拥有！";
		
		 msg.thumbs=['imgs/100x100.png','imgs/code.png'];
	 	msg.pictures=['imgs/code.png','imgs/100x100.png'];
	}else{
		 msg.pictures=['imgs/code.png','imgs/100x100.png'];
		 
	}
	// 发送分享
	if(sb.s.authenticated){
		//	mui.toast('---已授权---');
		shareMessage(msg, sb.s);
	}else{
			mui.toast('---未授权---');
		sb.s.authorize(function(){
			shareMessage(msg,sb.s);
		}, function(e){
			//	mui.toast('认证授权失败：'+e.code+' - '+e.message);
		});
	}
}
/**
   * 发送分享消息
   * @param {JSON} msg
   * @param {plus.share.ShareService} s
   */
function shareMessage(msg, s){
	//outLine(JSON.stringify(msg));
	s.send(msg, function(){
		mui.toast('分享到"'+s.description+'"成功！');
	}, function(e){
			//mui.toast('分享到"'+s.description+'"失败: '+JSON.stringify(e));
	});
}


/**
 * 调用系统分享
  */
function shareSystem(){
	 
	var msg={content:"好物，折扣尽在寐淘"};
	 
    msg.pictures=['imgs/xuanchuan.png'];
	 
	plus.share.sendWithSystem?plus.share.sendWithSystem(msg, function(){
		 
		//console.log('Success');
	}, function(e){
		 
		console.log('Failed: '+JSON.stringify(e));
	}):shareSystemNativeJS();
}
function shareSystemNativeJS() {
	if(plus.os.name!=='Android'){
		mui.toast('此平台暂不支持系统分享功能!') 
		return;
	}
	var intent=new Intent(Intent.ACTION_SEND);
	if(pic&&pic.realUrl){
		var p = '';
		p = pic.realUrl;
		if(p.substr(0,7)==='file://'){
			p=p.substr(7);
		}else if(p.sub(0)!=='/'){
			p=plus.io.convertLocalFileSystemURL(p);
		}
	}
	var f = new File(p);
	var uri = Uri.fromFile(f);
	if(f.exists()&&f.isFile()){
		console.log('image/*');
		intent.setType('image/*');
		intent.putExtra(Intent.EXTRA_STREAM,uri);
	}else{
		console.log('text/plain');
		intent.setType('text/plain');
	}
	intent.putExtra(Intent.EXTRA_SUBJECT,'HelloH5');
	intent.putExtra(Intent.EXTRA_TEXT,sharecontent.value);
	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	main.startActivity(Intent.createChooser(intent,'寐淘app'));
}